//
//  WebCamera.m
//  UnityExchangeTest
//
//  Created by qnpocket on 2019/5/7.
//  Copyright © 2019 qnpocket. All rights reserved.
//

#import "WebCamera.h"
#import <AVFoundation/AVFoundation.h>
#import <AVFoundation/AVCaptureDevice.h>
#import "UIKit/UIKit.h"

@implementation WebCamera

extern "C"
{

    void IOS_RequestCamAuthorization()
    {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied)
        {
            // 无权限
            
        }
        
    }

    bool IOS_CheckCamAuthorization()
    {
        AVAuthorizationStatus status = [AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeVideo];
        if (status == AVAuthorizationStatusRestricted || status == AVAuthorizationStatusDenied)
        {
            return false;
            // 无权限
            // do something...sss
        }
        else if(status == AVAuthorizationStatusAuthorized){
            return true;
        }
        else
            return false;
    }
    
    void IOS_JumpToSetPanel()
    {
        NSURL *url1 = [NSURL URLWithString:@"App-Prefs:root=Privacy&path=CAMERA"];
        // iOS10也可以使用url2访问，不过使用url1更好一些，可具体根据业务需求自行选择
        NSURL *url2 = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
        if (@available(iOS 11.0, *)) {
            if ([[UIApplication sharedApplication] canOpenURL:url2]){
                [[UIApplication sharedApplication] openURL:url2 options:@{} completionHandler:nil];
            }
        } else {
            if ([[UIApplication sharedApplication] canOpenURL:url1]){
                if (@available(iOS 10.0, *)) {
                    [[UIApplication sharedApplication] openURL:url1 options:@{} completionHandler:nil];
                } else {
                    [[UIApplication sharedApplication] openURL:url1];
                }
            }
        }
    }
}

@end
